package onlinediary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import onlinediary.entity.Diary;
import onlinediary.entity.Group;
import onlinediary.entity.User;
import onlinediary.service.IDiaryService;
import onlinediary.service.IGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2022-05-03
 */
@CrossOrigin
@Controller
@RequestMapping("/diary")
public class DiaryController {
    @Resource
    private IDiaryService diaryService;

    @Resource
    private IGroupService groupService;

    @GetMapping("/diaryDetail")
    public String diaryDetail(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        Integer uId = user.getId();
        List<Diary> list = diaryService.list(
                new QueryWrapper<Diary>()
                        .lambda()
                        .eq(Diary::getUId, uId)
        );
        list.forEach(diary -> {
            Group group = groupService.getById(diary.getGId());
            String gName = group.getName();
            diary.setGName(gName);
        });
        model.addAttribute("diaries", list);
        return "diaryDetail";
    }

    @GetMapping("/delete")
    public String delete(Integer id, Integer gId) {
        System.out.println(id);
        System.out.println(gId);
        boolean b = diaryService.removeById(id);
        Group group = groupService.getById(gId);
        if (group.getDcount() > 0) {
            group.setDcount(group.getDcount() - 1);
            groupService.updateById(group);
        }
        return "redirect:/diary/diaryDetail";
    }

    @PostMapping("/update")
    public String update(Diary diary) {
        boolean b = diaryService.updateById(diary);
        return "redirect:/diary/diaryDetail";
    }

    @GetMapping("/updatePage")
    public String updatePage(Integer id, Integer gId,
                             Model model) {
        Diary diary = diaryService.getById(id);
        Group group = groupService.getById(gId);
        String gName = group.getName();
        diary.setGName(gName);

        List<Group> groups = groupService.list();

        model.addAttribute("groups", groups);
        model.addAttribute("diary", diary);
        model.addAttribute("u", 1);
        return "addMessage/addDiary";
    }

    @GetMapping("/insertPage")
    public String insertPage(Model model, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<Group> groups = groupService.list(
                new QueryWrapper<Group>()
                        .lambda()
                        .eq(Group::getUId, user.getId())
        );

        model.addAttribute("groups", groups);
        return "addMessage/insertDiary";
    }

    @PostMapping("/insert")
    public String insert(Diary diary, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        Integer gId = diary.getGId();
        Group group = groupService.getById(gId);
        group.setDcount(group.getDcount() + 1);
        group.setUId(user.getId());
        groupService.updateById(group);
        diary.setUId(user.getId());
        diaryService.save(diary);
        return "redirect:/diary/diaryDetail";
    }
}
